<template>
	<view class="active-page">
		<!-- <button open-type='getUserInfo' >获取授权</button> -->
		<view class="" style="color: #ffffff;" v-if="detail!=''" v-html="detail"></view>
		<!-- <image src="https://tool.tuanhaoke.cn/static/image/home/thk.jpg" mode="widthFix"></image> -->
		<view class="gain-vip">
			<view class="">会员领取限时/限量</view>
			<!-- #ifdef H5 -->
			<view class="bottom" @click="gainFreeVIP">立刻领取7天免费会员</view>
			<!-- #endif -->
			<!-- #ifdef MP-WEIXIN -->
			<button class="bottom" type="primary" withCredentials="true" lang="zh_CN" @click="getuserinfo">立刻领取7天免费会员</button>
			<!-- #endif -->
		</view>
	</view>
</template>

<script>
//#ifdef H5
import wx from '../../../common/wx-js-sdk.js';
//#endif
import store from '../../../store/index.js';
import { testLogin, userLogin, newInfo, login } from '@/common/login.js';
export default {
	data() {
		return {
			userInfo: '',
			code: '',
			nickName: '',
			avatarUrl: '',
			store_id: '',
			page_data: '',
			detail:'',
			shopShare:'',
			page:'',
			share_id:'',
			option:''
		};
	},
	onLoad(options) {
		if(options.option){
			this.option=JSON.parse(options.option)
		}
		
		if( options.shopShare == '1' ){
			this.$store.commit('updataShare', 1)
		}
		console.log( options.share_id,"？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？");
		this.page = options.page
		this.shopShare = options.shopShare
		let store_id = options.store_id
		console.log('8888888');
		this.getAdvertisementImg()
		
		if( options.share_id ){
			// #ifdef MP-WEIXIN
				this.share_id = parseInt(options.share_id,10);
				this.store_id = parseInt(options.store_id,10);
			// #endif
		}
		if(store_id && !this.share_id){
			uni.redirectTo({
				url:'/pages/recommend/recommend?store_id=' + store_id
			})
		}
	},
	onShow() {
		if (this.$store.getters.getToken) {
			if( this.share_id ){
				
			}else{
				this.gainUserStore()
			}
		}
	},
	methods: {
		getAdvertisementImg(){
			let _self = this
			uni.request({
				url: this.$store.state.URL + '/thk/store_member/find_store_member',
				data:{
					token:'',
				},
				method:"POST",
				success(res) {
					console.log(res,'999999999999');
					_self.detail = res.data.data.detail.replace(/\<img/gi, '<img style="max-width:100%;height:auto" ');
					console.log(_self.detail,"？？？？？？？？？？？？？？？？？？？？");
				},
				fail(res) {
					console.log(res,'失败');
					console.log(data);
				}
			})
			// let data = {
			// 	token:'',
			// }
			// console.log("？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？");
			// this.$api.get_advertisementImg(data).then(res => {
			// 	console.log(res)
			// 	if(res.status){
			// 		this.detail = res.data.detail.replace(/\<img/gi, '<img style="max-width:95%;height:auto" ');
			// 		console.log(this.detail,"？？？？？？？？？？？？？？？？？？？？");
			// 	}
			// }).catch(err => {
			// 	console.log(err)
			// })
		},
		async getuserinfo() {
			console.log("123456")
			if(this.share_id){
				//#ifdef MP-WEIXIN
				await userLogin(this.share_id,this.store_id).then((res)=>{
					console.log(res,"9876")
					console.log(555555)
					console.log(this.$store.getters.getToken,"97654")
					if(this.option){
							uni.navigateTo({
							url:'/pages/memberCenter/specialGroup/invite/inviteStaff?actId='+this.option.actId+'&page_type='+this.option.page_type
						})
					}else{
						this.gainShopId()
					}
					
				});
				//#endif
			}else{
				//#ifdef MP-WEIXIN
				await userLogin().then((res)=>{
					console.log(res,"9876")
					console.log(555555)
					console.log(this.$store.getters.getToken,"97654")
					if(this.option){
							uni.navigateTo({
							url:'/pages/memberCenter/specialGroup/invite/inviteStaff?actId='+this.option.actId+'&page_type='+this.option.page_type
						})
					}else{
						this.gainShopId()
					}
				});	
				//#endif
			}
		},
		gainUserStore() {
			let data = {
				token: this.$store.getters.getToken,
				// #ifdef H5
					type:2
				// #endif
				// #ifdef MP-WEIXIN
					type:1
				// #endif
			};
			console.log('get_user_store',data);
			this.$api.get_user_store(data).then(res => {
				console.log('get_user_store',res);
				if (res.status) {
					this.$store.commit('updateStoreId',res.data);
					this.$store.commit('updataShare', 1)
					uni.setStorageSync("shopID",res.store_id)
					console.log(this.$store.getters.getStoreId,'this.$store.getters.getShopId')
					
					if(this.$store.getters.getStoreId && !this.share_id){
						uni.redirectTo({
							url:'/pages/recommend/recommend?store_id=' + this.$store.getters.getStoreId
						})
					}
					if( this.share_id ){
						this.gainFreeVIP()
					}
				} else {
					this.gainShopId()
				}
			})
			.catch(err => {
				console.log(err);
			});
		},
		gainHomeData() {
			let data = {
				token: this.$store.getters.getToken
			};
			this.$api
				.gian_home_data(data)
				.then(res => {
					console.log(res);
					if (res.status) {
						this.page_data = JSON.parse(res.list.content);
						console.log(this.page_data, '1111111');
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				})
				.catch(err => {
					console.log(err);
				});
		},
		gainShopId() {
			// console.log('gainShopId',this.token)
			console.log("我进来了")
			let data = {
				token:this.$store.getters.getToken,
			}
			console.log(data,'gainShopId data')
			this.$api.judge_store(data).then(res => {
				console.log(res,'judge_store')
				if(res.status){
					this.$store.commit('updataShare', 1)
					// this.$store.commit('updataOrganId',res.customer_organization_id);
					this.$store.commit('updateStoreId',res.store_id);
					// uni.setStorageSync("OrganId",res.customer_organization_id)
					uni.setStorageSync("shopID",res.store_id)
					console.log(this.$store.getters.getStoreId,'this.$store.getters.getShopId')
					if(this.$store.getters.getStoreId && !this.share_id){
						uni.redirectTo({
							url:'/pages/recommend/recommend?store_id=' + this.$store.getters.getStoreId
						})
					}
					if( this.share_id ){
						this.gainFreeVIP()
					}
				}else{
					// #ifdef H5
						this.gainFreeVIP()
					// #endif
					// #ifdef MP-WEIXIN
					if( this.share_id ){
						this.gainFreeVIP()
					}else{
						uni.redirectTo({
							url:'/pages/recommend/recommend?store_id=' + this.$store.getters.getStoreId
						})
					}
					// #endif
				}
			}).catch(err => {
				console.log(err,'this.$api.judge_store')
			})
		},
		//#ifdef H5
		gainFreeVIP() {
			uni.setStorageSync("vip_share_id",this.share_id)
			uni.setStorageSync("vip_page",'share')
			uni.setStorageSync("vip_store_id",this.store_id)
			this.$store.commit('updataShare', 1)
			if (this.$store.getters.getToken) {
				uni.navigateTo({
					url: '/pages/home/registeredShop?shared_customer_id=' + this.shared_customer_id + '&share_id=' + this.share_id + '&page=share&store_id=' + this.store_id,
					// url:'setOrganization'
				});
			} else {
				// location.href = this.$store.state.URL + '/customer/user/wx_login?page=/pages/home/organization/setOrganization&shared_customer_id=' + this.shared_customer_id;
				location.href = this.$store.state.URL + '/customer/user/wx_login?page=/pages/home/registeredShop&shared_customer_id=' + this.shared_customer_id;
			}
		},
		//#endif
		//#ifdef MP-WEIXIN
		gainFreeVIP() {
			console.log("gainFreeVIP")
			uni.navigateTo({
				url: '/pages/home/registeredShop?shared_customer_id=' + this.shared_customer_id + '&share_id=' + this.share_id + '&page=share&store_id=' + this.store_id,
			});
		},
		//#endif
		testLogin(share_user_id, fromJin) {
			// console.log("验证了一次")
			return new Promise((resolve, reject) => {
				uni.showLoading({
					title: '授权登录中'
				});
				let self = this;
				uni.request({
					url: store.state.URL + '/login/Login/mini_program_authorization',
					data: {
						code: this.code,
						iv: this.userInfo.iv,
						encryptedData: this.userInfo.encryptedData,
						share_user_id: share_user_id,
						fromJin: fromJin
					},
					method: 'POST',
					success(res) {
						uni.hideLoading();
						resolve();
					}
				});
				console.log(this.data, '1111111111111');
			});
		}
	}
};
</script>

<style>
page {
	background: linear-gradient(to bottom, #f86c49, #f99466);
}
.active-page {
	width: 100%;
	height: 100%;
}
.active-page > image {
	width: 100%;
	height: 188upx;
}
.vip-details {
	width: 690upx;
	/* height: 1146upx; */
	background: #ffffff;
	margin: 0 auto;
	border-radius: 20upx;
	border: 4upx solid #ffdc34;
	padding: 30upx 20upx;
	box-sizing: border-box;
	box-shadow: 0 4upx 22upx 0 rgba(248, 41, 45, 0.39);
	margin-bottom: 150upx;
}
.vip-introduce > image {
	width: 100%;
	height: 366upx;
	margin: 20upx 0;
}
.vip-introduce-title {
	font-size: 30upx;
}
.vip-introduce-text {
	font-size: 28upx;
}
.gain-vip {
	width: 100%;
	height: 100upx;
	background: #ffffff;
	box-shadow: 0 -4upx 24upx 0 rgba(65, 29, 0, 0.12);
	border-radius: 16upx 16upx 0 0;
	display: flex;
	justify-content: space-around;
	position: fixed;
	bottom: 0;
	left: 0;
}
.gain-vip > view:first-child {
	color: #999999;
	font-size: 24upx;
	line-height: 100upx;
	margin-left: 30upx;
}
.gain-vip .bottom {
	width: 456upx;
	height: 80upx;
	line-height: 80upx;
	margin-top: 10upx;
	background: linear-gradient(to right, #fe9543, #ff664f);
	border-radius: 40upx;
	text-align: center;
	font-size: 30upx;
	color: #ffffff;
}
</style>
